package com.ftg.midamall.service;

/**
 * @author 王子璇
 * @version 1.0
 * @date 2023/11/16 10:51
 */
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.RespMessUtil;
import com.ftg.midamall.util.FileUtils;
import com.ftg.midamall.util.TipUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.UUID;

/**
 * 业务上传
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/11/16 上午9:36
 */
@Service
@Slf4j
public class UploadService {

    /**
     * 上传头像
     *
     * @param avatarFile 头像文件
     * @return 处理结果
     */
    public RespMessUtil<JSONObject> uploadAvatar(MultipartFile avatarFile) {
        byte[] fileContent;
        JSONObject result = new JSONObject();
        try {
            fileContent = avatarFile.getBytes();

            if (fileContent.length > TipUtils.AVATAR_FILE_SIZE) {
                throw new RuntimeException(TipUtils.AVATAR_FILE_SIZE_TIP);
            }

            String extension = FileUtils.getExtension(avatarFile);
            if (!Arrays.asList(FileUtils.IMAGE_EXTENSION).contains(extension.toLowerCase())) {
                throw new RuntimeException(TipUtils.AVATAR_FILE_TYPE_TIP);
            }

            String newFileName = TipUtils.getAvatarFileName(extension);
            String uploadFilePath = TipUtils.AVATAR_FILE_PATH;
            String ipAddress = InetAddress.getLocalHost().getHostAddress();

            FileUtil.writeBytes(fileContent, new File(uploadFilePath + newFileName));

            result.put(TipUtils.RESULT_AVATAR_IP, ipAddress);
            result.put(TipUtils.RESULT_AVATAR_FILENAME, newFileName);
            result.put(TipUtils.RESULT_AVATAR_PATH, uploadFilePath);
            result.put(TipUtils.RESULT_AVATAR_URL, TipUtils.getUrl(ipAddress, newFileName));

            return RespMessUtil.build(CodeStatusEnum.SUCCESS, result);
        } catch (IOException e) {
            result.put(TipUtils.RESULT_AVATAR_NAME, TipUtils.AVATAR_FILE_FAILURE);
            result.put(TipUtils.RESULT_AVATAR_FAIL_NAME, e.getMessage());
            log.error(TipUtils.AVATAR_FILE_FAILURE, e);
            return RespMessUtil.build(CodeStatusEnum.FAILURE, result);
        }
    }


    /**
     * 删除头像
     *
     * @param avatarFileUrl 头像文件的URL
     * @return 删除结果的响应消息
     */
    public RespMessUtil<JSONObject>   delAvatar(String avatarFileUrl) {
        //5f818854-1b39-4255-b632-4cdab6ca6d5a.png
        String fileName = FileUtils.getFileUrl(avatarFileUrl);
        System.out.println(fileName);
        //物理路径
        String fullName = TipUtils.AVATAR_FILE_PATH + fileName;
        System.out.println(fullName);
        JSONObject result = new JSONObject(2);

        if (FileUtil.exist(fullName)) {
            FileUtil.del(fullName);
            result.put(TipUtils.RESULT_AVATAR_NAME, TipUtils.RESULT_SUCCESS_DEL);
            result.put(TipUtils.RESULT_OLD_FILE_NAME, avatarFileUrl);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS, result);
        } else {
            result.put(TipUtils.RESULT_AVATAR_NAME, TipUtils.RESULT_NO_EXIST);
            return RespMessUtil.build(CodeStatusEnum.FAILURE, result);
        }
    }
}